

if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)
  
  function launchAppEnhance(app_name) {
    var thread = threads.start(function () {
      toastLog("检查是否有打开权限!")
      while (true) {
        var allow = text("允许").findOne(5000)
        if (allow) {
          sleep(1000)
          toastLog("发现允许授权!")
          allow.click()
          sleep(1000)
          thread.interrupt()
          break
        }
      }
    })
    sleep(1000)
    var result = launchApp(app_name)
    sleep(3000)
    thread.interrupt()
    return result
  }
   
  function task() {
    //   var notice = id("notice_delete").findOne(2000);
    //   if(notice){
    //     notice.click();
    //   }else{
    //     log('没有通知');
    //   }
      console.log('开始task')
      sleep(10000);
      let videoCount = 0
      let thread1 = threads.start(function () {
        while (true) {
          console.log('true')
          var done = className('android.view.View').text('3/3').findOne(2000)
          if (done) {
            console.log('找到3-3')
            ui.run(function(){
              toastLog('任务已完成')
            });
            thread1.interrupt()
          }else{
              // utils.swip_down();
              console.log('找到广告控件')
              sleep(2000);
              var countView = className("android.view.View").depth(13).drawingOrder(0).indexInParent(2).findOne(2000);
              var finalActivity = currentActivity()
              点击控件(countView);
              videoCount++
              if(videoCount*30 < 100){
                utils.updateAppProgress(videoCount*30);
              }             
              utils.waitAdvert(finalActivity);
          }
          sleep(2000);
        }
      })
  }
  


  // 点击控件坐标，控件的clickbale为false的时候
function 点击控件(x) {
  var weight = x.bounds()
  click(weight.centerX(), weight.centerY())
}
  
  
  
  function main() {
    let open_app = launchAppEnhance("广告汪");
    if (open_app) {
      task()
    }
    else {
      toastLog("广告汪打开失败，请确认是否安装并授权打开!");
    }
  }
  
  try {
    main();
    utils.updateAppProgress(100);
  } catch (error) {
    toastLog(error);
  }
  
  storage.put("script_running_status", "end");
  